package com.beone.smart.userCenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beone.smart.userCenter.model.entity.SystemUserInfoDO;
import com.beone.smart.userCenter.model.result.ResultEntity;
import com.beone.smart.userCenter.model.result.ResultTool;
import com.beone.smart.userCenter.service.intf.SysUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Date: 2019/8/27
 * @Author: yangjie
 * desc:  系统级别用户接口
 **/
@Api(tags = "系统级别用户接口")
@RestController
@RequestMapping(value = "/sys_user_info")
@Slf4j
public class SysUserController {
    @Autowired
    private SysUserInfoService sysUserInfoService;

    @ApiOperation(value = "通过ticket获取用户", notes = "现在是简单测试")
    @GetMapping(value = "/sys_user_by_key")
    public ResultEntity getSysUserInfoByKey(@RequestParam @ApiParam(value = "用户的凭证") String ticketKey
    ) {
        QueryWrapper<SystemUserInfoDO> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(SystemUserInfoDO::getTicket_key, ticketKey)
                .eq(SystemUserInfoDO::getEnabled, 1);
        SystemUserInfoDO systemUserInfoDO = sysUserInfoService.getOne(wrapper);
        log.info("传入的凭证是{},取出的ticketKey是{}", ticketKey, ticketKey);
        if (systemUserInfoDO != null) {
            return ResultTool.ok(systemUserInfoDO, systemUserInfoDO.getTicket_key());
        } else {
            return ResultTool.fail();
        }
    }

    @ApiOperation(value = "为用户创建ticket", notes = "现在是简单测试")
    @GetMapping(value = "/make_ticket")
    public ResultEntity makeTicketKey(@RequestParam @ApiParam(value = "用户来源") String origin
    ) {
        return ResultTool.ok(sysUserInfoService.makeTicket(origin));
    }
}
